package com.ktjy.service.impl;

import com.ktjy.entity.SysUser;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> getSysUserList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    @Transactional
    @Override
    public int saveBatch(List<SysUser> list) {
        int rows=0;
        for (int i = 0; i < list.size() ; i++) {
            SysUser sysUser=list.get(i);
            if(i==1){
                throw new RuntimeException("数据库异常");
            }
            int rs=sysUserMapper.insert(sysUser);
            rows+=rs;
        }
        return rows;
    }

    @Override
    public SysUser login(String account, String password) {
        //判断用户名是否正确
        SysUser sysUser = sysUserMapper.getByAccount(account);
        if(sysUser!=null){//如果有该账号
            //判断密码是否正确
            if(password.equals(sysUser.getPassword())){
                return sysUser;
            }
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser sysUser, Integer pageNo, Integer pageSize) {
        Page<SysUser> page=new Page<>();
        //设置当前的页码数
        page.setPageNo(pageNo);
        //设置每页的条数
        page.setPageSize(pageSize);
        //设置总记录数
        Integer count=sysUserMapper.count(sysUser);
        page.setTotalCount(count);
        //设置每页的数据
        List<SysUser> list = sysUserMapper.getPage(sysUser, page.offset(), page.getPageSize());
        page.setData(list);

        return page;
    }

    @Override
    @Transactional
    public boolean save(SysUser sysUser) {
        return sysUserMapper.insert(sysUser)>0;
    }

    @Override
    public SysUser findById(Long id) {
        return sysUserMapper.getById(id);
    }

    @Override
    public boolean update(SysUser sysUser) {
        return sysUserMapper.update(sysUser)>0;
    }

    @Override
    public boolean delete(Long id) {
        return sysUserMapper.delete(id)>0;
    }

    @Override
    public SysUser findByAccount(String account) {
        return sysUserMapper.getByAccount(account);
    }
}
